﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UniversityManagementSystem.DAL;

namespace UniversityManagementSystem.UI
{
    public partial class CourseEntry : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if(!IsPostBack)
                {
                    Department department = new Department();
                    Semester semester = new Semester();
                    List<Department> allDepartments = new List<Department>();
                    List<Semester> allSemesters = new List<Semester>();
                    allSemesters = semester.GetAllSemesters();
                    allDepartments = department.GetAllDepartments();
                    departmentDropDownList.DataSource = allDepartments;
                    departmentDropDownList.DataTextField = "DepartmentName";
                    departmentDropDownList.DataValueField = "DepartmentCode";
                    departmentDropDownList.DataBind();
                    semesterDropDownList.DataSource = allSemesters;
                    semesterDropDownList.DataTextField = "SemesterName";
                    semesterDropDownList.DataValueField = "SemesterId";
                    semesterDropDownList.DataBind();
                }
            }
            catch (Exception)
            {
                confirmationMessage.Text = "Error occured while loading the page.";
                confirmationMessage.ForeColor = System.Drawing.Color.Red;
            }
        }

        protected void saveButton_Click(object sender, EventArgs e)
        {
            string saveMessage = "";
            try
            {
                Course aCourse = new Course();
                string courseCredit = "";
                courseCredit = courseCreditTextBox.Text;
                aCourse.CourseTitle = courseTitleTextBox.Text;
                aCourse.CourseName = courseNameTextBox.Text;
                aCourse.CourseDescription = courseDescriptionTextBox.Text;
                aCourse.CourseDepartment = departmentDropDownList.SelectedValue;
                aCourse.CouresSemester = Convert.ToInt32(semesterDropDownList.SelectedValue);
                saveMessage = aCourse.SaveCourse(courseCredit);
                confirmationMessage.Text = saveMessage;
                confirmationMessage.ForeColor = System.Drawing.Color.Blue;

            }
            catch (Exception ex)
            {
                confirmationMessage.Text = "An internel error occured.";
                confirmationMessage.ForeColor = System.Drawing.Color.Red;
            }
            
        }
    }
}